using UnityEngine;
using UnityEngine.UI;
using QFramework;
using DG.Tweening;
using System;

namespace HareGame
{
    public class UIFingerData : UIPanelData
    {
        public Action OnClick;
        public Vector3 Pos;
        public Vector3 Offset;
        public int ClickTimes = 1;
        public bool IsWorldPoint = false;
        public UIFingerType MaskType = UIFingerType.Mask;
    }
    public partial class UIFinger : UIBase
    {
        protected override void OnInit(IUIData uiData = null)
        {
            base.OnInit();
            mData = uiData as UIFingerData ?? new UIFingerData();
            Mask.DOFade(0.7f, 0.25f).SetEase(Ease.Linear);

            if (mData.IsWorldPoint)
                mData.Pos = GameEntry.Util.WorldPointToUIPoint(mData.Pos, btnFinger.GetComponent<RectTransform>());

            switch (mData.MaskType)
            {
                case UIFingerType.None:
                    Mask.gameObject.SetActive(false);
                    break;
                case UIFingerType.UIItem:
                    Fouce.gameObject.SetActive(false);
                    break;
            }

            Fouce.transform.position = mData.Pos;
            btnFinger.transform.position = mData.Pos;
            btnFinger.transform.localPosition += mData.Offset;
            btnFinger.onClick.AddListener(() =>
            {
                mData.OnClick?.Invoke();
                mData.ClickTimes--;
                if (mData.ClickTimes == 0)
                    GameEntry.UI.CloseUI(this);
            });
        }

        protected override void OnOpen(IUIData uiData = null)
        {
            base.OnOpen();
        }


        protected override void OnClose()
        {
            base.OnClose();

            btnFinger.onClick.RemoveAllListeners();
        }
    }
}
